package app.poller.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;
import app.poller.R;
import app.poller.entities.Contact;

public class ContactAutoCompleteArrayAdapter extends ArrayAdapter<Contact>{
	
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private ArrayList<Contact> mContactList;
	private ArrayList<Contact> mContactListAll;
	private ArrayList<Contact> mSuggestions;

	@SuppressWarnings("unchecked")
	public ContactAutoCompleteArrayAdapter(Context context, int textViewResourceId, ArrayList<Contact> objects) {
		super(context, textViewResourceId, objects);
		mContext = context;
		mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mContactList = objects;
		mContactListAll = (ArrayList<Contact>) objects.clone();
		mSuggestions = new ArrayList<Contact>();
	}
	
	@Override
	public Filter getFilter() {
		return nameFilter;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh;
		
		if(convertView == null) {
			vh = new ViewHolder();
			convertView = mLayoutInflater.inflate(R.layout.contact_autocomplete_row, parent, false);
			vh.contactNameView = (TextView) convertView.findViewById(R.id.contact_name);
			vh.contactPhoneNumberView = (TextView) convertView.findViewById(R.id.contact_number);
			convertView.setTag(vh);
		}
		else {
			vh = (ViewHolder) convertView.getTag();
		}
		
		vh.contactNameView.setText(mContactList.get(position).getContactName());
		vh.contactPhoneNumberView.setText(mContactList.get(position).getContactPhoneNumber());
		
		return convertView;
	}
	
	
	public static class ViewHolder {
		TextView contactNameView;
		TextView contactPhoneNumberView;
	}	
	
	Filter nameFilter = new Filter() {
        
		public String convertResultToString(Object resultValue) {
            String str = ((Contact)(resultValue)).getContactName() + " <" + ((Contact)(resultValue)).getContactPhoneNumber() + ">";
            return str;
        }
        
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if(constraint != null) {
                mSuggestions.clear();
                for (Contact contact: mContactListAll) {
                    if(contact.getContactName().toLowerCase().startsWith(constraint.toString().toLowerCase())){
                        mSuggestions.add(contact);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = mSuggestions;
                filterResults.count = mSuggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }
        
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            ArrayList<Contact> filteredList = (ArrayList<Contact>) results.values;
            if(results != null && results.count > 0) {
                clear();
                for (Contact c : filteredList) {
                    add(c);
                }
                notifyDataSetChanged();
            }
        }
    };
}
